const md5 = require('md5'); //md5加密
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
// 创建一个JSDOM实例
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`);
global.window = dom.window;
global.document = dom.window.document;
global.navigator = dom.window.navigator;
const getSignature  = require('./webmssdk'); // 假设你已经将webmssdk.js导出为一个函数

const dyConfig = {
    baseWssUrl: 'wss://webcast5-ws-web-lq.douyin.com/webcast/im/push/v2/?',
    userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0',
    versionCode: 180800,
    webcastSdkVersion: '1.0.14-beta.0',
    liveRoomId: '', // 直播房间ID
    userUniqueId: '', // 用户唯一ID
}

class douyinWssInfo {
    //构造函数
    constructor() { }

    //设置参数
	setConfig(config) {
		if(!config) return this
		Object.assign(dyConfig, config)
		return this
	}

    //获取抖音 WSS 连接地址
    async getWssUrl(){
        const signature = await this.getXMsStubSignature();
        const splitUserAgent = dyConfig.userAgent.split('/');
        const webcast5Params = {
            app_name: 'douyin_web',
            version_code: dyConfig.versionCode,
            webcast_sdk_version: dyConfig.webcastSdkVersion,
            update_version_code: dyConfig.webcastSdkVersion,
            compress: 'gzip',
            device_platform: 'web',
            cookie_enabled: true,
            screen_width: 1440,
            screen_height: 960,
            browser_language: 'zh-CN',
            browser_platform: 'Win32',
            browser_name: splitUserAgent[0].split(' ')[0],
            browser_version: dyConfig.userAgent.substring(dyConfig.userAgent.indexOf(splitUserAgent[0].split(' ')[0]) + splitUserAgent[0].split(' ')[0].length + 1),
            browser_online: true,
            tz_name: 'Asia/Shanghai',
            host: 'https://live.douyin.com',
            aid: 6383,
            live_id: 1,
            did_rule: 3,
            endpoint: 'live_pc',
            support_wrds: 1,
            user_unique_id: dyConfig.userUniqueId,
            im_path: '/webcast/im/fetch/',
            identity: 'audience',
            room_id: dyConfig.liveRoomId,
            heartbeatDuration: 0,
            signature: signature
        };
        const tmpArr = [];
        Object.keys(webcast5Params).forEach((key) => {
            tmpArr.push(`${key}=${webcast5Params[key]}`);
        });
        return dyConfig.baseWssUrl + tmpArr.join('&');
    }

    //签名第1步 生成 X-Bogus
    async getXMsStubSignature(){
        const sigParams = {
            live_id: 1,
            aid: 6383,
            version_code: dyConfig.versionCode,
            webcast_sdk_version: dyConfig.webcastSdkVersion,
            room_id: dyConfig.liveRoomId,
            sub_room_id: '',
            sub_channel_id: '',
            did_rule: 3,
            user_unique_id: dyConfig.userUniqueId,
            device_platform: 'web',
            device_type: '',
            ac: '',
            identity: 'audience'
        };
        const tmpArr = [];
        Object.keys(sigParams).forEach((key) => {
            tmpArr.push(`${key}=${sigParams[key]}`);
        });
        const md5Res = md5(tmpArr.join(','));
        return this.getWsSignature(md5Res);
    }

    //签名第2步 生成 ws连接 签名  生成函数 md5后的字符
    async getWsSignature(md5Str){
        let signature = '';
        try {
            signature = getSignature(md5Str);
        } catch (err) {}
        return signature;
    }

}
module.exports = douyinWssInfo